<template>
  <div>
    <TitleSecondary :title="title" />
    <div
      id="funnel-box"
      class="funnel"
      :style="{ width: width, height: height }"
    ></div>
  </div>
</template>

<script>
export default {
  components: {},
  props: {
    width: {
      type: String,
      default: '300px',
    },
    height: {
      type: String,
      default: '300px',
    },
    option: {
      type: Object,
      default: () => ({}),
    },
    title: {
      type: String,
      default: '漏斗',
    },
    series: {
      type: Array,
      default: () => ([]),
    },
  },
  data() {
    return {
      myChart: null,
    };
  },
  computed: {
    getSeries() {
      return this.series || [];
    },
    yData1() {
      let sortList = [];
      let sortName = [];
      // eslint-disable-next-line vue/no-side-effects-in-computed-properties
      sortList = this.getSeries.sort((a, b) => {
        return b.value - a.value;
      });
      sortName = sortList.map((item) => {
        return item.name;
      }).reverse();
      console.log(sortList, this.series, sortName)
      return sortName;
    },
    yData2() {
      let sortList = [];
      let sortRatio = [];
      // eslint-disable-next-line vue/no-side-effects-in-computed-properties
      sortList = this.getSeries.sort((a, b) => {
        return b.value - a.value;
      });
      sortRatio = sortList.map((item) => {
        return item.ratio;
      }).reverse();
      return sortRatio;
    },
  },
  watch: {},
  created() {},
  mounted() {
    this.$nextTick(() => {
      this.initFunnel()
      window.addEventListener('resize', this.initPie);
    });
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.initFunnel);
  },
  methods: {
    initFunnel() {
      this.myChart = this.$echarts.init(document.getElementById('funnel-box'));
      this.getSeries.length && this.setOption();
      // this.setOption();
      this.myChart.resize({
        // width: document.getElementById('summary').offsetWidth
      });
    },
    setOption() {
      let option = {
        // title: this.option.title,
        grid: {
          left: '16%',
          right: '16%'
        },
        tooltip: this.option.tooltip,
        xAxis: {
          show: false
        },
        yAxis: [
          {
            type: 'category',
            name: '销售阶段',
            nameGap: 20,
            nameLocation: 'end',
            nameTextStyle: {
              fontWeight: '400',
              fontSize: 14,
              align: 'right',
              color: '#000000',
              padding: [0, 6 ,0 , 0]
            },
            axisTick: {
              show: false
            },
            data: this.yData1
          },
          {
            type: 'category',
            axisTick: {
              show: false
            },
            axisLine: {
              show: false
            },
            data: this.yData2
          },
        ],
        series: [
          {
            type: 'funnel',
            left: '13%',
            right: '13%',
            top: 60,
            bottom: 60,
            width: 'auto',
            height: '72%',
            min: 0,
            max: 100,
            minSize: '2%',
            maxSize: '100%',
            sort: 'descending',
            // sort: 'none',
            gap: 10,
            label: {
              color: '#fff',
              show: true,
              position: 'inside',
              formatter: '{c}'
            },
            labelLine: {
              show: false,
            },
            itemStyle: {
              color: '#5b8ff9',
              borderColor: '#fff',
              borderWidth: 1,
              // opacity: 0.85
            },
            emphasis: {
              label: {
                fontSize: 20,
              },
            },
            markPoint: {
              symbol: 'rect'
            },
            data: this.getSeries,
          },
        ],
      };

      this.myChart.on('click', params => {
        // console.log(params);
        this.$emit('changeRegion', params);
      });

      option && this.myChart.setOption(option);
    },
  },
};
</script>

<style lang="less" scoped>
// /deep/.container {
//   margin: 0;
// }
</style>
